import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
import sys
sys.path.append("./")
from matplotlib.pyplot import MultipleLocator
legend_font = {'family': 'Arial',
               'weight': 'normal',
               'size': 14,
               }

label_font = {'family': "Arial",
              'weight': 'normal',
              'size': 18,
              }

tick_font = {'family': 'Arial',
         'weight': 'normal',
         'size': 16
         }

title_font = {'family': 'Arial',
         'weight': 'normal',
         'size': 18}

marker_size = 10
line_width = 2
color_a = '#252525'
color_b = '#969696'
color_c = '#ca0020'
color_d = '#2171b5'
color_e = '#636363'

def plot_table1():
    # 1. read the latency data
    plt.rcParams["figure.figsize"] = [6, 4]
    fig, ax = plt.subplots()
    data1 = pd.read_excel("./data/table1.xlsx", index_col=0,sheet_name="308.15K")
    data2 = pd.read_excel("./data/table1.xlsx", index_col=0,sheet_name="323.15K")
    data3 = pd.read_excel("./data/table1.xlsx", index_col=0, sheet_name="338.15K")


    speed = np.arange(len(data1["speed"].values))
    ax.plot(speed, data1["heat"].values, "o--", color=color_a, markersize=marker_size, label="308.15K")
    ax.plot(speed, data2["heat"].values, "v--", color=color_c, markersize=marker_size, label="323.15K")
    ax.plot(speed, data3["heat"].values, "s--", color=color_d, markersize=marker_size, label="338.15K")

    ax.set_ylabel('Heat (J)', label_font)
    ax.set_xlabel('Flow Speed (M/S)', label_font)

    y_major_locator = MultipleLocator(15)
    x_major_locator = MultipleLocator(1)
    ax.yaxis.set_major_locator(y_major_locator)
    ax.xaxis.set_major_locator(x_major_locator)

    ax.set_xticks(speed)#set the location of xticks
    ax.set_xticklabels(data1["speed"].values) # set the name of each xtick
    # for tick in ax.get_xticklabels(): # to rotate the xticks
    # tick.set_rotation(45)
    ax.tick_params(labelsize=tick_font["size"])

    ax.set_ylim(15, 75)
    #ax.set_xlim(0.8, 8.2)

    ax.legend(prop=legend_font, loc='upper right')
    fig.tight_layout()
    # plt.grid(linestyle="--")
    plt.savefig("./image/table1.pdf", bbox_inches='tight', pad_inches=0)
    plt.show()
def plot_table2():
    plt.rcParams["figure.figsize"] = [6,4]
    fig, ax = plt.subplots(1,1)
    # 1. read all the latency ratio
    practial = pd.read_excel("./data/table2.xlsx",index_col=0,sheet_name="practical")
    input_water = np.around(np.array(practial["input_water"].values)-273.15,0).astype(np.int)
    practial_8 = practial["value_8"].values
    practial_50 = practial["value_50"].values
    practial_100 = practial["value_100"].values

    theory = pd.read_excel("./data/table2.xlsx", index_col=0, sheet_name="theory")
    theory_8 = theory["value_8"].values
    theory_50 = theory["value_50"].values
    theory_100 = theory["value_100"].values
    marker_shape = ['o','v','^','<','>','s','p','*']

    water_index = np.arange(0,len(input_water))

    plt.plot(water_index,practial_8,marker_shape[0]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="practical_8")
    plt.plot(water_index,practial_50,marker_shape[1]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="practical_50")
    plt.plot(water_index,practial_100,marker_shape[2]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="practical_100")

    plt.plot(water_index,theory_8,marker_shape[3]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="theory_8")
    plt.plot(water_index,theory_50,marker_shape[4]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="theory_50")
    plt.plot(water_index,theory_100,marker_shape[5]+"--",markersize=marker_size, markerfacecolor='none',
             linewidth=line_width,markeredgewidth=2,label="theory_100")

    ax.set_ylabel('Power (W)', label_font)
    ax.set_xlabel('Input Water Temperature ($^\circ$C)', label_font)
    # y_major_locator = MultipleLocator(0.05)
    # ax.yaxis.set_major_locator(y_major_locator)

    ax.set_xticks(np.arange(0,len(input_water)))
    ax.set_xticklabels(input_water, tick_font)
    '''
    for tick in ax.get_xticklabels():
        tick.set_rotation(45)
    '''
    ax.tick_params(axis="x",labelsize=tick_font["size"])
    ax.tick_params(axis="y",labelsize=tick_font["size"])

    ax.set_ylim(-40, 145)
    ax.yaxis.set_major_locator(MultipleLocator(40))
    ax.set_xlim(-0.3,len(input_water)-0.7)
    
    #ax.legend(prop=legend_font,ncol=1,loc='upper right',framealpha=0.05)#,loc='middle center'
    ax.legend(prop=legend_font,loc='upper left', columnspacing=0.1)
    fig.tight_layout()
    #fig.subplots_adjust(top=0.78)
    # plt.grid(linestyle="-.")
    plt.savefig("./image/table2.pdf")
    plt.show()



plot_table1()
